package com.ruoyi.my.service.impl.stream;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.my.service.stream.StreamService;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class StreamServiceImpl implements StreamService {

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Override
    public void common() {
        //获取字典列表信息
        SysDictData dictData = new SysDictData();
        List<SysDictData> list = dictDataMapper.selectDictDataList(dictData);

//遍历/匹配（foreach/find/match）
        //遍历输出符合条件的元素
        list.stream()
                .filter(e ->e.getIsDefault().equals("Y"))
                .forEach(System.out::println);

        //匹配筛选后的第一个值
        Optional<SysDictData> findFirstSysDictData = list.stream()
                .filter(e ->e.getIsDefault().equals("Y"))
                .findFirst();
        System.out.println("匹配筛选后的第一个值" + findFirstSysDictData.get());

        // 匹配任意（适用于并行流）
        Optional<SysDictData> findAnySysDictData = list.parallelStream()
                .filter(e ->e.getIsDefault().equals("Y"))
                .findAny();
        System.out.println("匹配任意" + findAnySysDictData.get());

        //是否包含符合条件的数据
        boolean anyMatch = list.stream()
                .anyMatch(e ->e.getIsDefault().equals("Y"));
        System.out.println("是否包含符合条件的数据" + anyMatch);

        //筛选
        List<String> fiterList = list.stream()
                .filter(e ->e.getIsDefault().equals("Y"))
                .map(SysDictData::getDictValue)
                .collect(Collectors.toList());
        System.out.println("筛选符合条件的数据并打印其值" + fiterList);

//聚合（max/min/count)
        Optional<SysDictData> maxDictCode = list.stream()
                .max(Comparator.comparing(SysDictData::getDictCode));
        System.out.println("code最大值" + maxDictCode);

        //统计数量
        long count = list.stream()
                .filter(x ->x.getIsDefault().equals("N"))
                .count();
        System.out.println("N的数量" + count);

        //计算和
        long sum = list.stream()
                .mapToLong(SysDictData::getDictSort)
                .sum();
        System.out.println("sort的和" + sum);

        //根据类型分组
        Map<String, List<SysDictData>> mapType = list.stream()
                .collect(Collectors.groupingBy(SysDictData::getDictType));
        System.out.println("根据类型分组" + mapType);

        //是否默认分组
        Map<Boolean, List<SysDictData>> mapIsDefault = list.stream()
                .collect(Collectors.partitioningBy(x ->x.getIsDefault().equals("Y")));
        System.out.println("是否默认分组" + mapIsDefault);

        //先根据类型分组，再根据默认值分组
        Map<String,Map<String, List<SysDictData>>> map = list.stream()
                .collect(Collectors.groupingBy(SysDictData::getDictType,Collectors.groupingBy(SysDictData::getIsDefault)));
        System.out.println("先根据类型分组，再根据默认值分组" + map);
    }
















}
